﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGolovkine.Core.Diagnostics;

namespace IGolovkine.Core
{
    public class ObjectDisposedCheck : IDisposable
    {
        private readonly object _source;
        private volatile bool _isDisposed;

        public ObjectDisposedCheck(object source)
        {
            Debug.NotDefault(() => source);
            _source = source;
            _isDisposed = false;
        }

        public void Dispose()
        {
            this.EnsureNotDisposed();
            _isDisposed = true;
        }

        public bool IsDisposed
        {
            get { return _isDisposed; }
        }

        public void EnsureNotDisposed()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(_source.GetType().FullName);
            }
        }
    }
}
